package com.zhixianggou.caibaobox.lottery.dataCollect.utils;

import com.zhixianggou.caibaobox.user.entities.User;
import org.apache.http.NameValuePair;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * @author zyq
 * 8/25/24 5:42 PM
 */
public class CookieUtil {
    /**
     * 获取cookie
     * @param url
     * @return cookie
     */
    public static String getCookie(String url,String userAgent) {
        //时间戳
        long timestamp = new Date().getTime();
        //请求地址
        HttpClient client = new HttpClient();
        //post请求方式
        PostMethod postMethod = new PostMethod(url);

        //这里是设置请求内容为json格式,根据站点的格式决定
        //因为这个网站会将账号密码转为json格式,所以需要这一步
        postMethod.setRequestHeader("Content_Type","application/json");
        postMethod.setRequestHeader("user-agent",userAgent);
        //执行请求
        try {
            client.executeMethod(postMethod);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //通过Post/GetMethod对象获取响应头信息
        String cookie = postMethod.getResponseHeader("Set-Cookie").getValue();
        return cookie;
//        return cookie.split(";")[0] + ";";
    }

    public static void main(String[] args) throws IOException {
//        String cookie = getCookie("https://www.cwl.gov.cn");
        String cookie = getCookie("https://www.cwl.gov.cn", UserAgentUtil.randomUserAgent());
        System.out.println(cookie);
    }
}
